package cn.group01.git.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.group01.git.common.CodeMsg;
import cn.group01.git.common.ResponseResult;
import cn.group01.git.dto.GoodsPageDTO;
import cn.group01.git.entity.Goods;
import cn.group01.git.mapper.GoodsMapper;
import cn.group01.git.service.GoodsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
/**
 * @description:
 * @author: 笪志林
 * @date: 2023/7/13 15:52
 **/
@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;


    public ResponseResult getList(GoodsPageDTO goodsPageDTO) {
        PageHelper.startPage(goodsPageDTO.getPage(), goodsPageDTO.getLimit());
        List<Goods> goods = goodsMapper.getList(goodsPageDTO.getGoodsName(), goodsPageDTO.getSupName());
        PageInfo<Goods> goodsPageInfo = new PageInfo<Goods>(goods);
        List<Goods> list = goodsPageInfo.getList();
        long total = goodsPageInfo.getTotal();
        return ResponseResult.success(CodeMsg.SUCCESS, list, total);
    }


    public ResponseResult batchAdd(MultipartFile file) {
        if (file.isEmpty()) {
            return ResponseResult.fail(CodeMsg.EMPTY_FILE_ERROR);
        }
        //解析excel表格
        try {
            InputStream inputStream = file.getInputStream();
            //导入参数
            ImportParams importParams = new ImportParams();
            importParams.setTitleRows(1);
            importParams.setHeadRows(1);
            List<Goods> goodss = ExcelImportUtil.importExcel(inputStream, Goods.class, importParams);
            for (Goods goods : goodss) {
                String goodsName = goods.getGoodsName();
                Goods goods1 = goodsMapper.getByGoodsName(goodsName);
                if (!ObjectUtils.isEmpty(goods1)) {
                    throw new RuntimeException("商品重复");
                }
                //添加商品到数据库
                goodsMapper.addGoods(goods);
            }
            return ResponseResult.success(CodeMsg.SUCCESS);

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("服务器异常");
        }
    }


    public void export(String supName, HttpServletResponse response) {
        //根据供应商查询商品信息
        List<Goods> goods = goodsMapper.getBySupName(supName);
        ExportParams exportParams = new ExportParams("商品信息表", supName);
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Goods.class, goods);
        try {
            //  设置响应的格式  以附件形式打开
            response.setContentType("application/force-download");// 设置强制下载不打开
            response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("商品信息表.xls", "UTF-8"));// 设置文件名
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public ResponseResult batchDelete(Integer[] ids) {
        goodsMapper.batchDelete(ids);
        return ResponseResult.success(CodeMsg.SUCCESS);
    }


    public ResponseResult editGoods(Goods goods) {
        goodsMapper.editGoods(goods);
        return ResponseResult.success(CodeMsg.SUCCESS);
    }


    public Goods getByGoodsName(String goodsName) {
        return goodsMapper.getByGoodsName(goodsName);
    }


    public ResponseResult addGoods(Goods goods) {
        Goods goodsName = goodsMapper.getByGoodsName(goods.getGoodsName());
        if (!ObjectUtils.isEmpty(goodsName)){
            return ResponseResult.fail(CodeMsg.GOODS_EXIT);
        }
        //添加用户信息
        goodsMapper.addGoods(goods);
        return ResponseResult.success(CodeMsg.SUCCESS);
    }


    public List<Goods> selectGoodsName() {
        System.out.println(goodsMapper.selectGoodsName());
        return goodsMapper.selectGoodsName();
    }

}